//
//  BaiKeMainTableViewHeaderView.m
//  BaseProject
//
//  Created by mac on 2020/12/22.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BaiKeMainTableViewHeaderView.h"
#import "BaiKeMainModel.h"
#import "BaiKeSecondClassesVC.h"
@interface BaiKeMainCollectionCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *iconIV;
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)BaiKeMainClassesDataModel *listModel;
@end

@implementation BaiKeMainCollectionCell
- (void)setListModel:(BaiKeMainClassesDataModel *)listModel
{
    _listModel = listModel;
    [self.iconIV sd_setImageWithURL:listModel.icon.wppURL placeholderImage:[UIImage imageNamed:@"placeholder"]];
    self.titleLab.text = listModel.oneName;
}
#pragma mark - lazy懒加载
- (UIImageView *)iconIV
{
    if (!_iconIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self addSubview:iv];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        iv.clipsToBounds = YES;
        iv.frame = CGRectMake((self.width - 46*ADAPTER_WIDTH) * 0.5, 14*ADAPTER_WIDTH, 46*ADAPTER_WIDTH, 46*ADAPTER_WIDTH);
        _iconIV = iv;
    }
    return _iconIV;
}
- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = rgba(34, 34, 34, 1);
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(0, self.iconIV.bottom + 5*ADAPTER_WIDTH, self.width, 17*ADAPTER_WIDTH);
        _titleLab = lab;
    }
    return _titleLab;
}
@end

@interface BaiKeMainTableViewHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *mainView;
@property(nonatomic,strong)UIView *progressGreyView;
@property(nonatomic,strong)UIView *progressRedView;
@end

@implementation BaiKeMainTableViewHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = WhiteColor;
    }
    return self;
}
- (void)setMainModel:(BaiKeMainModel *)mainModel
{
    _mainModel = mainModel;
    //测试
    /**
     NSMutableArray *array = [NSMutableArray array];
     for (NSInteger index = 0; index < 22; index ++) {
         [array addObject:mainModel.data.classes.firstObject];
     }
     mainModel.data.classes = array;
     */
    [self.mainView reloadData];
    [self progressGreyView];
    [self progressRedView];
    if (self.mainModel.data.classes.count <= 10) {
        self.progressGreyView.width = 16*ADAPTER_WIDTH;
        self.progressGreyView.left = (self.width - self.progressGreyView.width) * 0.5;
        self.progressRedView.width = self.progressGreyView.width;
    }
}
#pragma mark - UICollectionView代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mainModel.data.classes.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BaiKeMainCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = WhiteColor;
    cell.contentView.backgroundColor = WhiteColor;
    cell.listModel = self.mainModel.data.classes[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WK(weakSelf)
    [SVProgressHUD showWithStatus:@""];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.mainModel.data.classes[indexPath.row].scId forKey:@"scId"];
    [TTHttpManager postRequestWithUrl:[NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,@"/userWeddingEncyclopedia/queryWedAllClass"] TimeOut:10 PostData:parameters FinishBlock:^(id  _Nonnull responseObject) {
        //NSLog(@"%@",responseObject);
        BaiKeMainClassesModel *responModel = [BaiKeMainClassesModel modelWithJSON:responseObject];
        if (responModel.success) {
            [SVProgressHUD dismiss];
            BaiKeSecondClassesVC *vc = [[BaiKeSecondClassesVC alloc]init];
            vc.classesModel = responModel;
            vc.oneId = weakSelf.mainModel.data.classes[indexPath.row].scId;
            vc.oneName = weakSelf.mainModel.data.classes[indexPath.row].oneName;
            [weakSelf.parentVC.navigationController pushViewController:vc animated:YES];
        } else {
            [SVProgressHUD showWithStatus:responModel.message];
            [SVProgressHUD dismissWithDelay:1];
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD showWithStatus:@"请检查网络~"];
        [SVProgressHUD dismissWithDelay:1];
    }];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat maxOffset = self.mainView.contentSize.width  - self.mainView.width;
    CGFloat progress = scrollView.contentOffset.x / maxOffset;
    self.progressRedView.left = (self.progressGreyView.width - self.progressRedView.width) * progress;
}
#pragma mark - Lazy懒加载区域
- (UICollectionView *)mainView
{
    if (!_mainView) {
        CGFloat orinX = 7*ADAPTER_WIDTH,itemWidth = floor((self.width - (2 * orinX)) * 0.2),itemHeight = floor(82*ADAPTER_WIDTH);//14*ADAPTER_WIDTH + 46*ADAPTER_WIDTH + 22*ADAPTER_WIDTH
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
        flowLayout.minimumLineSpacing = 0*ADAPTER_WIDTH;
        flowLayout.minimumInteritemSpacing = 0*ADAPTER_WIDTH;//就一行
        flowLayout.sectionInset = UIEdgeInsetsMake(0, orinX, 0, orinX);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,  0*ADAPTER_WIDTH, self.width, 165*ADAPTER_WIDTH) collectionViewLayout:flowLayout];
        [self addSubview:collectionView];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [collectionView registerClass:[BaiKeMainCollectionCell class] forCellWithReuseIdentifier:@"Cell"];
        collectionView.backgroundColor = [UIColor whiteColor];
        //collectionView.pagingEnabled = YES;
        collectionView.showsHorizontalScrollIndicator = NO;
        _mainView = collectionView;
    }
    return _mainView;
}
- (UIView *)progressGreyView
{
    if (!_progressGreyView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = rgba(232, 232, 232, 1);
        view.frame = CGRectMake((self.width - 36*ADAPTER_WIDTH) * 0.5, self.mainView.bottom + 13*ADAPTER_WIDTH , 36*ADAPTER_WIDTH, 2.5*ADAPTER_WIDTH);
        [self addSubview:view];
        view.layer.cornerRadius = 1.25*ADAPTER_WIDTH;
        view.layer.masksToBounds = YES;
        _progressGreyView = view;
    }
    return _progressGreyView;
}
- (UIView *)progressRedView
{
    if (!_progressRedView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = rgba(255, 66, 66, 1);
        view.frame = CGRectMake(0, 0 , 16*ADAPTER_WIDTH, 2.5*ADAPTER_WIDTH);
        [self.progressGreyView addSubview:view];
        view.layer.cornerRadius = 1.25*ADAPTER_WIDTH;
        view.layer.masksToBounds = YES;
        _progressRedView = view;
    }
    return _progressRedView;
}
@end
